//
//  AreaPick.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/17.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "AreaPick.h"

@interface AreaPick () <UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickView;
@property (nonatomic, strong) NSArray *provices;
@property (nonatomic, strong) NSArray *citys;
@property (nonatomic, strong) NSArray *areas;
@property (nonatomic, strong) NSDictionary *pickData;
@property (nonatomic, strong) NSArray *selectedArray;
@property (nonatomic, strong) NSString *provice;
@property (nonatomic, strong) NSString *city;
@property (nonatomic, strong) NSString *area;





@end

@implementation AreaPick

+ (instancetype)areaPick {
    AreaPick *area = [[[NSBundle mainBundle] loadNibNamed:@"AreaPick" owner:nil options:nil] lastObject];
    return area;
}


- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupView];
}

- (void)setupView {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    self.pickData = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.provices = [self.pickData allKeys];
    
    self.selectedArray = [self.pickData objectForKey:[self.provices objectAtIndex:0]];
    
    if (self.selectedArray.count > 0) {
        self.citys = [[self.selectedArray objectAtIndex:0] allKeys];
    }
    
    if (self.citys.count > 0) {
        self.areas = [[self.selectedArray objectAtIndex:0] objectForKey:[self.citys objectAtIndex:0]];
    }
    self.pickView.delegate = self;
    self.pickView.dataSource = self;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
    [self addGestureRecognizer:tap];
}

- (void)hide {
    if ([self.delegate respondsToSelector:(@selector(cancelChoice))]) {
        [self.delegate cancelChoice];
    }
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if(component == 0) {
        return self.provices.count;
    }else if(component == 1) {
        return self.citys.count;
    }else if(component == 2) {
        return self.areas.count;
    }
    return -1;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

    if(component == 0) {
        return self.provices[row];
    }else if(component == 1){
        return self.citys[row];
    }else if(component ==2) {
        return self.areas[row];
    }
    return @"其他";
}


- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UILabel *pickLabel = (UILabel *)view;
    if(!pickLabel) {
        pickLabel = [[UILabel alloc] init];
        pickLabel.minimumScaleFactor = 8.;
        pickLabel.adjustsFontSizeToFitWidth = YES;
        [pickLabel setTextAlignment:NSTextAlignmentCenter];
        [pickLabel setBackgroundColor:[UIColor clearColor]];
        [pickLabel setFont:[UIFont boldSystemFontOfSize:13]];
        
    }
    pickLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickLabel;
}

#pragma mark 选中行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if(component == 0) { // 如果是0列被选中  那么1，2 列也要变化
        NSString *selectedProvice = self.provices[row];
        self.selectedArray = [self.pickData objectForKey:selectedProvice];
        if(_selectedArray.count > 0) {
            self.citys = [[self.selectedArray objectAtIndex:0] allKeys];
        }else {
            self.citys = nil;
        }
        
        if (self.citys.count > 0) {
            self.areas = [[self.selectedArray objectAtIndex:0] objectForKey:[self.citys objectAtIndex:0]];
        }else {
            self.areas = @[@"其他地区"];
        }
        
        [pickerView selectRow:0 inComponent:1 animated:YES];
        [pickerView reloadComponent:1];
        [pickerView selectRow:0 inComponent:2 animated:YES];
        [pickerView reloadComponent:2];
        
    }else if(component == 1) {
        NSString *selectedCity = self.citys[row];
        
        self.areas = [[self.selectedArray objectAtIndex:0] objectForKey:selectedCity];
        [pickerView selectRow:1 inComponent:2 animated:YES];
        [pickerView reloadComponent:2];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}


- (IBAction)cancelPick:(id)sender {
    if ([self.delegate respondsToSelector:(@selector(cancelChoice))]) {
        [self.delegate cancelChoice];
    }
}

- (IBAction)completePick:(id)sender {
    self.provice =[self.provices objectAtIndex:[self.pickView selectedRowInComponent:0]];
    self.city = [self.citys objectAtIndex:[self.pickView selectedRowInComponent:1]];
    self.area = [self.areas objectAtIndex:[self.pickView selectedRowInComponent:2]];
    if ([self.delegate respondsToSelector:(@selector(commitArea:city:area:))]) {
        [self.delegate commitArea:self.provice city:self.city area:self.area];
    }
}


@end
